

#import "FAMMineWeightViewController.h"

@interface FAMMineWeightViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *FAMScrollView;
@property (weak, nonatomic) IBOutlet UILabel *FAMCurrentWeightLabel;
@property (weak, nonatomic) IBOutlet UILabel *FAMWeightOneLabel;
@property (weak, nonatomic) IBOutlet UILabel *FAMWeightTwoLabel;
@property (weak, nonatomic) IBOutlet UILabel *FAMWeightThreeLabel;

@property (weak, nonatomic) IBOutlet UIView *FAMPNLineCharBackView;

@property (nonatomic, strong)PNLineChart *FAMLineChart;
//@property (nonatomic, strong)PNLineChartData *FAMData01;

@end

@implementation FAMMineWeightViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self FAM_setWeight];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self FAM_showUi];
}
- (void)FAM_showUi{
    self.title = @"Current Weight";
    self.FAMScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    [self FAM_setPNLineChart];
}
- (void)FAM_setWeight{
    self.FAMCurrentWeightLabel.attributedText = [FAMTool FAM_AttributedString:[FAMUserManger shareInstance].userModel.weight styleTwoString:@"kg" styleColorOne:kCustom0xColor(0xF7A912, 1.0) styleColorTwo:kCustom0xColor(0x191919, 1.0) styleFontOne:40 styleFontTwo:34];
    self.FAMWeightOneLabel.attributedText = [FAMTool FAM_AttributedString:[NSString nullToString:[FAMUserManger shareInstance].userModel.inits_weight] styleTwoString:@"kg" styleColorOne:kCustom0xColor(0xF7A912, 1.0) styleColorTwo:kWhiteColor styleFontOne:24 styleFontTwo:17];
    self.FAMWeightTwoLabel.attributedText = [FAMTool FAM_AttributedString:[NSString nullToString:[FAMUserManger shareInstance].userModel.weight] styleTwoString:@"kg" styleColorOne:kCustom0xColor(0xF7A912, 1.0) styleColorTwo:kWhiteColor styleFontOne:24 styleFontTwo:17];
    self.FAMWeightThreeLabel.attributedText = [FAMTool FAM_AttributedString:[NSString nullToString:[FAMUserManger shareInstance].userModel.target_weight] styleTwoString:@"kg" styleColorOne:kCustom0xColor(0xF7A912, 1.0) styleColorTwo:kWhiteColor styleFontOne:24 styleFontTwo:17];
}
- (void)FAM_setPNLineChart{
    NSMutableArray *famDateArray = [NSMutableArray array];
    NSMutableArray *weightArray = [NSMutableArray array];
    for (int i = 0; i < [FAMUserManger shareInstance].userModel.weights.count; i++) {
        FAMWeightsModel *model = [FAMUserManger shareInstance].userModel.weights[i];
        NSString *fmtDate = [model.fmt_date substringWithRange:NSMakeRange(4, 4)];
        NSMutableString *fmtDateMutableString = [[NSMutableString alloc]initWithString:fmtDate];
        [fmtDateMutableString insertString:@"/" atIndex:2];
        [famDateArray addObject:fmtDateMutableString];
        [weightArray addObject:model.weight];
    }
    
    self.FAMLineChart = [[PNLineChart alloc]initWithFrame:CGRectMake(0, 40, self.FAMPNLineCharBackView.frame.size.width, self.FAMPNLineCharBackView.frame.size.height - 40)];
    [self.FAMLineChart setXLabels:famDateArray];
    self.FAMLineChart.backgroundColor = kClearColor;
    self.FAMLineChart.showYGridLines = YES;
    self.FAMLineChart.showYGridLines = YES;
    self.FAMLineChart.showCoordinateAxis = YES;
    self.FAMLineChart.axisColor = kCustom0xColor(0x191919, 1.0);
    self.FAMLineChart.axisWidth = 2.0f;
    [self.FAMLineChart setYLabels:@[@"50",@"80",@"110",@"140",@"170",@"200"]];
    PNLineChartData *FAMData01 = [PNLineChartData new];
    FAMData01.color = PNFreshGreen;
    FAMData01.itemCount = self.FAMLineChart.xLabels.count;
    FAMData01.inflexionPointStyle = PNLineChartPointStyleCircle;
    FAMData01.inflexionPointColor = kCustom0xColor(0x01FFFF, 1.0);
    FAMData01.showPointLabel = YES;
    FAMData01.pointLabelColor = kCustom0xColor(0x191919, 1.0);
    FAMData01.pointLabelFont = [UIFont systemFontOfSize:12];
    FAMData01.pointLabelFormat = @"%0.1f";
    FAMData01.getData = ^(NSUInteger index) {
        CGFloat yValue = ((NSString *)weightArray[index]).floatValue;
        return [PNLineChartDataItem dataItemWithY:yValue];
    };
    self.FAMLineChart.chartData = @[FAMData01];
    [self.FAMLineChart strokeChart];
    [self.FAMPNLineCharBackView addSubview:self.FAMLineChart];
}
- (IBAction)FAM_addWeightButtonClick:(UIButton *)sender {
    FAMMineAddWeightView *mineAddWeightView =  [[FAMMineAddWeightView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    WeakSelf(self);
    mineAddWeightView.block = ^{
        [weakSelf FAM_setWeight];
        [weakSelf FAM_setLineChart];
    };
    [self.view addSubview:mineAddWeightView];
}
- (void)FAM_setLineChart{
    [self.FAMLineChart removeFromSuperview];
    NSMutableArray *famDateArray = [NSMutableArray array];
    NSMutableArray *weightArray = [NSMutableArray array];
    for (int i = 0; i < [FAMUserManger shareInstance].userModel.weights.count; i++) {
        FAMWeightsModel *model = [FAMUserManger shareInstance].userModel.weights[i];
        NSString *fmtDate = [model.fmt_date substringWithRange:NSMakeRange(4, 4)];
        NSMutableString *fmtDateMutableString = [[NSMutableString alloc]initWithString:fmtDate];
        [fmtDateMutableString insertString:@"/" atIndex:2];
        [famDateArray addObject:fmtDateMutableString];
        [weightArray addObject:model.weight];
    }
    
    self.FAMLineChart = [[PNLineChart alloc]initWithFrame:CGRectMake(0, 40, self.FAMPNLineCharBackView.frame.size.width, self.FAMPNLineCharBackView.frame.size.height - 40)];
    [self.FAMLineChart setXLabels:famDateArray];
    self.FAMLineChart.backgroundColor = kClearColor;
    self.FAMLineChart.showYGridLines = YES;
    self.FAMLineChart.showYGridLines = YES;
    self.FAMLineChart.showCoordinateAxis = YES;
    self.FAMLineChart.axisColor = kCustom0xColor(0x191919, 1.0);
    self.FAMLineChart.axisWidth = 2.0f;
    [self.FAMLineChart setYLabels:@[@"50",@"80",@"110",@"140",@"170",@"200"]];
    PNLineChartData *FAMData01 = [PNLineChartData new];
    FAMData01.color = PNFreshGreen;
    FAMData01.itemCount = self.FAMLineChart.xLabels.count;
    FAMData01.inflexionPointStyle = PNLineChartPointStyleCircle;
    FAMData01.inflexionPointColor = kCustom0xColor(0x01FFFF, 1.0);
    FAMData01.showPointLabel = YES;
    FAMData01.pointLabelColor = kCustom0xColor(0x191919, 1.0);
    FAMData01.pointLabelFont = [UIFont systemFontOfSize:12];
    FAMData01.pointLabelFormat = @"%0.1f";
    FAMData01.getData = ^(NSUInteger index) {
        CGFloat yValue = ((NSString *)weightArray[index]).floatValue;
        return [PNLineChartDataItem dataItemWithY:yValue];
    };
    self.FAMLineChart.chartData = @[FAMData01];
    [self.FAMLineChart strokeChart];
    [self.FAMPNLineCharBackView addSubview:self.FAMLineChart];
    
}
- (IBAction)FAM_weightOneTapClick:(UITapGestureRecognizer *)sender {
    FAMMineLastAndTargetWeightViewController *vc = [[FAMMineLastAndTargetWeightViewController alloc]init];
    vc.FAMIsAmend = YES;
    vc.FAMAmendWeight = @"init_weight";
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (IBAction)FAM_weightTwoTapClick:(UITapGestureRecognizer *)sender {
    FAMMineLastAndTargetWeightViewController *vc = [[FAMMineLastAndTargetWeightViewController alloc]init];
    vc.FAMIsAmend = YES;
    vc.FAMAmendWeight = @"weight";
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)FAM_weightThreeTapClick:(UITapGestureRecognizer *)sender {
    FAMMineLastAndTargetWeightViewController *vc = [[FAMMineLastAndTargetWeightViewController alloc]init];
    vc.FAMIsAmend = YES;
    vc.FAMAmendWeight = @"target_weight";
    [self.navigationController pushViewController:vc animated:YES];
}
@end
